//package com.csx.data.sample.security;
//
//import com.csx.data.sample.security.impl.DefaultDBSecurity;
//import org.springframework.cloud.bootstrap.config.PropertySourceLocator;
//import org.springframework.core.annotation.Order;
//import org.springframework.core.env.CompositePropertySource;
//import org.springframework.core.env.Environment;
//import org.springframework.core.env.MapPropertySource;
//import org.springframework.core.env.PropertySource;
//import org.springframework.stereotype.Component;
//
//import java.util.HashMap;
//import java.util.Map;
//
//
///**
// * 加密数据库密码
// * Created by zhangshuanchao on 2018/11/6.
// */
//@Order(0)
//@Component
//public class DBPropertySourceLocator implements PropertySourceLocator {
//
//    @Override
//    public PropertySource<?> locate(Environment environment) {
//        // 1、mysql/oracle数据库配置解密
//        String enabled = environment.getProperty("spring.datasource.encrypt.enabled");
//        Map<String, Object> configMap = new HashMap<>();
//        if ("true".equals(enabled)) {
//            //获取原始密码
//            String password = environment.getProperty("spring.datasource.password");
//
//            //去除右括号
//            if (password.contains(")")) {
//                password = password.replace(")", "");
//            }
//            //数据库密码解密
//            String[] keyAndPassword = password.split("\\(");
//            if (keyAndPassword.length == 2) {   //数据源存在加解密配置
//                DBSecurity security = new DefaultDBSecurity();
//                password = security.decrypt(keyAndPassword[1]);
//            } else {   //数据源不存在加解密配置
//                password = keyAndPassword[0];
//            }
//
//            //设置数据库的配置
//            configMap.put("spring.datasource.password", password);
//        }
//
//        //将配置加入spring容器中
//        CompositePropertySource composite = new CompositePropertySource("dbConfig");
//        composite.addPropertySource(new MapPropertySource("db", configMap));
//        return composite;
//    }
//}
